import { FC, useEffect } from 'react'
import { Form, Input, Checkbox } from 'antd'
import { propsType } from './index'
import { useDispatch } from 'react-redux'
import { editComponent } from '@/store/componentsReducer'
const { TextArea } = Input
const PropComponent: FC<propsType> = props => {
  const { text, isCenter, disabled } = props
  const [form] = Form.useForm()
  const dispatch = useDispatch()
  useEffect(() => {
    form.setFieldsValue({ text, isCenter })
  }, [text, isCenter, form])
  const handelValueChange = () => {
    dispatch(editComponent(form.getFieldsValue()))
  }
  return (
    <Form
      onValuesChange={handelValueChange}
      disabled={disabled}
      layout="vertical"
      initialValues={{ text, isCenter }}
      form={form}>
      <Form.Item label="段落内容" name="text" rules={[{ required: true, message: '请输入段落内容' }]}>
        <TextArea autoSize={{ minRows: 2 }} />
      </Form.Item>
      <Form.Item name="isCenter" valuePropName="checked">
        <Checkbox>居中显示</Checkbox>
      </Form.Item>
    </Form>
  )
}

export default PropComponent
